package com.example.mall

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.example.mall.entity.CartDTO
import com.example.mall.entity.GoodsInfo
import com.example.mall.network.CartService
import com.example.mall.network.GoodsService
import com.example.mall.network.ServiceCreator
import com.example.mall.util.ToastUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class DetailActivity : AppCompatActivity(), View.OnClickListener {
    var tv_title : TextView? = null
    var tv_count : TextView? = null
    var tv_goods_price : TextView? = null
    var tv_goods_desc : TextView? = null
    var iv_goods_pic : ImageView? = null
    var mGoodsId : Int? = null
    var app : MyApplication?= null
    val cartService = ServiceCreator.create<CartService>()
    val goodsService = ServiceCreator.create<GoodsService>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        app = application as MyApplication
        setContentView(R.layout.activity_detail)
        tv_title = findViewById(R.id.tv_title)
        tv_count = findViewById(R.id.tv_count)
        tv_goods_price = findViewById(R.id.tv_goods_price)
        tv_goods_desc = findViewById(R.id.tv_goods_desc)
        iv_goods_pic = findViewById(R.id.iv_goods_pic)
        findViewById<ImageView>(R.id.iv_back).setOnClickListener(this)
        findViewById<ImageView>(R.id.iv_cart).setOnClickListener(this)
        findViewById<Button>(R.id.btn_add_cart).setOnClickListener(this)

        tv_count?.setText(app?.cartCount.toString())

    }

    override fun onResume() {
        super.onResume()
        showDetail()
        tv_count?.setText(app?.cartCount.toString())
    }

    private fun showDetail() {
        mGoodsId = intent.getIntExtra("goods_id",0)
        if(mGoodsId!! > 0){
            goodsService.getGoodsById(mGoodsId!!).enqueue(object :
                Callback<GoodsInfo> {
                override fun onResponse(call: Call<GoodsInfo>,
                                        response: Response<GoodsInfo>
                ) {
                    val detail = response.body()
                    tv_title?.setText(detail?.name)
                    tv_goods_desc?.setText(detail?.description)
                    tv_goods_price?.setText(detail?.price?.toInt().toString())
                    Glide.with(this@DetailActivity).load(detail?.picPath).into(iv_goods_pic!!)
                }

                override fun onFailure(p0: Call<GoodsInfo>, p1: Throwable) {
                    ToastUtil.show(this@DetailActivity,"网络异常")
                }
            })
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.iv_back -> {
                finish()
            }
            R.id.iv_cart -> {
                val intent = Intent(this,CartActivity::class.java)
                startActivity(intent)
            }
            R.id.btn_add_cart -> {
                addToCart(mGoodsId!!)
            }
        }
    }

    fun addToCart(goodsId : Int){
        cartService.addToCart(CartDTO(app?.userId!!,goodsId)).enqueue(object :
            Callback<Int> {
            override fun onResponse(call: Call<Int>,
                                    response: Response<Int>
            ) {
                app?.cartCount = response.body()
                tv_count?.setText(app?.cartCount.toString())
            }

            override fun onFailure(p0: Call<Int>, p1: Throwable) {
                ToastUtil.show(this@DetailActivity,"网络异常")
            }
        })
    }

}